﻿using System.IO;
using System.IO.IsolatedStorage;
using System.Xml;
using System.Xml.Serialization;

namespace Kiva7.RatingReminders
{
    public class IsoStore
    {
        private const string FileName = "RatingReminderSettings.xml";

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static ReminderSettings LoadSettings()
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(FileName))
                    return null;

                using (var stream = new IsolatedStorageFileStream(FileName, FileMode.Open, FileAccess.Read, store))
                {
                    var serializer = new XmlSerializer(typeof (ReminderSettings));
                    var settings = (ReminderSettings) serializer.Deserialize(stream);
                    return settings;
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="settings"></param>
        public static void SaveSettings(ReminderSettings settings)
        {
            // Write to the Isolated Storage
            var xmlWriterSettings = new XmlWriterSettings {Indent = true};

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = new IsolatedStorageFileStream(FileName, FileMode.Create, FileAccess.Write, store))
                {
                    var serializer = new XmlSerializer(typeof(ReminderSettings));
                    using (var xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                    {
                        serializer.Serialize(xmlWriter, settings);
                    }
                }
            }
        }
    }
}